package com.example.chang.law_doc.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.chang.law_doc.R;
import com.example.chang.law_doc.entity.LawItemData;
import com.example.chang.law_doc.entity.SaleEntity;
import com.example.chang.law_doc.ui.ExplainActivity;
import com.example.chang.law_doc.ui.LibraryActivity;
import com.example.chang.law_doc.ui.SearchActivity;
import com.example.chang.law_doc.ui.TalkActivity;
import com.example.chang.law_doc.ui.TextShowActivity;
import com.example.chang.law_doc.utils.LawItemAdapter;
import com.example.chang.law_doc.utils.SaleAdapter;
import com.example.chang.law_doc.view.CustomDialog;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

import me.bakumon.library.view.BulletinView;

/**
 * Created by chang on 2018/2/22.
 * 项目描述：
 */

public class HomeFragement extends Fragment implements View.OnClickListener {
    private TextView tv_search;
    private TextView tv_library;
    private TextView tv_talk;
    private TextView tv_explain;
    private BulletinView mBulletinViewSale;
    public static final int []BANNER = new int[]{R.mipmap.lunbo1,R.mipmap.banner,R.mipmap.lunbo2};//再来一张
    private MZBannerView mMZBanner;
    public static HomeFragement newInstance(){
        return new HomeFragement();
    }
    private CustomDialog dialog;
    private ListView mListView;
    private List<LawItemData> mList = new ArrayList<>();
    private void initSaleDate(View view){
        mBulletinViewSale = (BulletinView) view.findViewById(R.id.bulletin_view_sale);
        final List<SaleEntity> saleEntities = new ArrayList<>();
        SaleEntity saleEntity = new SaleEntity();
        saleEntity.saleTitle = "重庆金虎物业管理...   ";
        saleEntity.salePrice = "物业纠纷";
        saleEntity.saleTag = "精品";
        saleEntity.saleImgRes = R.mipmap.tjyd1;

        SaleEntity saleEntity1 = new SaleEntity();
        saleEntity1.saleTitle = "陈刚商品房销售...       ";
        saleEntity1.salePrice = "房屋买卖合同纠纷";
        saleEntity1.saleTag = "推荐";
        saleEntity1.saleImgRes = R.mipmap.tjyd2;

        saleEntities.add(saleEntity);
        saleEntities.add(saleEntity1);
        mBulletinViewSale.setAdapter(new SaleAdapter(getActivity(),saleEntities));

    }
    private void initView(View view) {

        dialog=new CustomDialog(getActivity(),100,100,R.layout.dialog_text,R.style.Theme_dialog, Gravity.CENTER,R.style.pop_anim_style);
        //屏幕外点击无效
        dialog.setCancelable(false);

        mMZBanner = (MZBannerView) view.findViewById(R.id.banner);
        List<Integer> bannerList = new ArrayList<>();
        for(int i=0;i<BANNER.length;i++){
            bannerList.add(BANNER[i]);
        }
        mMZBanner.setPages(bannerList, new MZHolderCreator<HomeFragement.BannerViewHolder>() {
            @Override
            public HomeFragement.BannerViewHolder createViewHolder() {
                return new HomeFragement.BannerViewHolder();
            }
        });

        tv_search=view.findViewById(R.id.tv_search);
        tv_search.setOnClickListener(this);
        tv_explain=view.findViewById(R.id.tv_explain);
        tv_explain.setOnClickListener(this);
        tv_library=view.findViewById(R.id.tv_library);
        tv_library.setOnClickListener(this);
        tv_talk=view.findViewById(R.id.tv_talk);
        tv_talk.setOnClickListener(this);
        mListView = view.findViewById(R.id.mListView);
            LawItemData data = new LawItemData();
            data.setTitle("哈尔滨元盛房地产开发股份有...裁定书");
            data.setContent("再审申请人（一审被告、反诉原告、二审上诉人）：哈尔滨元盛房地产开发股份有限公司。住所地：黑龙江省哈尔滨市阿城区胜利街18委7组....");
            //data.setContent("被告人宋皓受贿一案，贵州省高级人民法院2010年4月2日作出（2010）黔高刑二终字第4号刑事判决，维持贵州省六盘水市中级人民法院.....");
            mList.add(data);
            LawItemData data1 = new LawItemData();
            data1.setTitle("佛山市丽园物业管理有限公司...裁定书");
            data1.setContent("申诉人（一审被告、二审上诉人、再审申请人）：广发银行股份有限公司开平支行，住所地广东省开平市长沙东兴大道人和东路1号六幢....");
            mList.add(data1);
            LawItemData data2 = new LawItemData();
            data2.setTitle("卢铁汉物业服务合同纠纷申诉...裁定书");
            data2.setContent("再审申请人（一审原告、二审上诉人）：卢铁汉，男，汉族，1994年1月22日出生，无业。 委托代理人：陈涛，北京市中盾律师事务所律师....");
            mList.add(data2);
            LawItemData data3 = new LawItemData();
            data3.setTitle("时代亮因与沈阳绿建物业服务...裁定书");
            data3.setContent("再审申请人（一审被告、二审上诉人）：时代亮被申请人（一审原告、二审被上诉人）：沈阳绿建物业服务有限公司奥园分公司。所在地....");
            mList.add(data3);


        LawItemAdapter adapter=new LawItemAdapter(getActivity(),mList);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i==0){
                    CountDownTimer timer1;
                    timer1=new CountDownTimer(1500,1000) {
                        @Override
                        public void onTick(long l) {
                            dialog.show();
                        }

                        @Override
                        public void onFinish() {
                            dialog.dismiss();
                            startActivity(new Intent(getActivity(), TextShowActivity.class));
                        }
                    };
                    timer1.start();

                }

            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.tv_library:
                startActivity(new Intent(getActivity(), LibraryActivity.class));
                break;
            case R.id.tv_talk:
                startActivity(new Intent(getActivity(), TalkActivity.class));
                break;
            case R.id.tv_explain:
                startActivity(new Intent(getActivity(), ExplainActivity.class));
                break;

        }
    }

    public static class BannerViewHolder implements MZViewHolder<Integer> {
        private ImageView mImageView;
        @Override
        public View createView(Context context) {
            // 返回页面布局文件
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item,null);
            mImageView = (ImageView) view.findViewById(R.id.banner_image);
            return view;
        }

        @Override
        public void onBind(Context context, int position, Integer data) {
            // 数据绑定
            mImageView.setImageResource(data);
        }
    }
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragement_home, null);
        initSaleDate(view);
        initView(view);
        return view;
    }
    @Override
    public void onPause() {
        super.onPause();
        mMZBanner.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mMZBanner.start();
    }

}
